/* eslint-disable */
<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <!-- 表格操作 -->
    <div class="operation-container">
      <!-- 条件筛选 -->
      <div class="operation-container">
        <el-radio-group v-model="weekDate" style="margin-right: 51rem" size="medium" @input="changeWeekDate">
          <el-radio-button label=0>上一周</el-radio-button>
          <el-radio-button label=1>本周</el-radio-button>
          <el-radio-button label=2>下一周</el-radio-button>
        </el-radio-group>
        <!-- 科室 -->
        <el-cascader :options="departmentList" v-model="departmentId" :show-all-levels="false"
                     placeholder="请选择科室" clearable size="small" style="margin-right:1rem" @change="cascaderChange"
        >
        </el-cascader>
        <el-input
            v-model="keywords"
            prefix-icon="el-icon-search"
            size="small"
            placeholder="请输入医生昵称"
            style="width:200px"
            @keyup.enter.native="searchUsers"
        />
        <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            style="margin-left:1rem"
            @click="searchUsers"
        >
          搜索
        </el-button>
      </div>
    </div>
    <!-- 表格展示 -->
    <el-table border :data="doctorScheduleList" v-loading="loading" max-height="750">
      <!-- 表格列 -->
      <el-table-column
          prop="avatar"
          label="头像"
          align="center"
          width="120"
          fixed
      >
        <template slot-scope="scope">
          <img :src=scope.row.avatar width="60" height="60"/>
        </template>
      </el-table-column>
      <el-table-column
          prop="doctorName"
          label="姓名"
          align="center"
          width="120"
          fixed
      />
      <el-table-column
          prop="departmentName"
          label="科室"
          align="center"
          width="140"
      />
      <!--      <el-table border :data="doctorScheduleList" v-loading="loading" max-height="750">-->
      <el-table-column
          label="星期一"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期一', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期一', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期一', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期一', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期一', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期一', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期一', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期一', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期二"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期二', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期二', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期二', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期二', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期二', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期二', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期二', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期二', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期三"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期三', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期三', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期三', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期三', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期三', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期三', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期三', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期三', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期四"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期四', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期四', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期四', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期四', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期四', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期四', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期四', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期四', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期五"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期五', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期五', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期五', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期五', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期五', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期五', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期五', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期五', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期六"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期六', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期六', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期六', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期六', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期六', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期六', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期六', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期六', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
          label="星期日"
          align="center"
          width="200"
      >
        <el-table-column
            label="上午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期日', 0, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期日', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期日', 0, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期日', 0, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="下午(号源)"
            align="center"
            width="100"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="getNumberSource('星期日', 1, scope.row.doctorScheduleDTO) == '休息'">
              {{ getNumberSource('星期日', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
            <el-tag type="danger" v-if="getNumberSource('星期日', 1, scope.row.doctorScheduleDTO) != '休息'">
              {{ getNumberSource('星期日', 1, scope.row.doctorScheduleDTO) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table-column>
      <!-- 列操作 -->
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
              type="primary"
              size="mini"
              @click="openModel(scope.row,2)"
          >
            <i class="el-icon-date"/>排班
          </el-button>
        </template>
      </el-table-column>
      <!--      </el-table>-->
    </el-table>
    <!-- 分页 -->
    <el-pagination
        class="pagination-container"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="current"
        :page-size="size"
        :total="count"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 排班操作框 -->
    <el-dialog :visible.sync="addScheduleShow" width="20%">
      <div class="dialog-title-container" slot="title" ref="scheduleTitle"/>
      <el-form label-width="60px" size="medium" :model="doctorScheduleForm">
        <el-form-item label="排班日期" required label-width="70">
          <!--  时间选择 -->
          <el-date-picker
              v-model="doctorScheduleForm.scheduleTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上午" label-width="50px">
          <!--  时间选择 -->
          <el-switch
              style="margin-left: 140px"
              v-model="doctorScheduleForm.amTimeQuantum"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="接诊"
              inactive-text="休息">
          </el-switch>
        </el-form-item>
        <el-form-item label="号源" required label-width="70" v-show="doctorScheduleForm.amTimeQuantum" >
          <el-input-number v-model="doctorScheduleForm.amNumberSource" :min="1" :max="100" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item label="下午" label-width="50px">
          <el-switch
              v-model="doctorScheduleForm.pmTimeQuantum"
              style="margin-left: 140px"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="接诊"
              inactive-text="休息">
          </el-switch>
        </el-form-item>
        <el-form-item label="号源" required label-width="70" v-show="doctorScheduleForm.pmTimeQuantum">
          <el-input-number v-model="doctorScheduleForm.pmNumberSource" :min="1" :max="100" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addScheduleShow = false">取 消</el-button>
        <el-button type="primary" @click="saveDoctorSchedule">
          确 定
        </el-button>
      </div>
    </el-dialog>


  </el-card>
</template>

<script>
export default {
  created() {
    this.listDoctorSchedule();
    this.listDepartments();
  },
  data: function () {
    return {
      scheduleDate: "",
      weekDate: 1,
      loading: true,
      addScheduleShow: false,
      //show: true,
      departmentIdList: [],
      departmentId: null,
      doctorScheduleList: [],
      departmentList: [],
      doctorScheduleForm: {
        doctorId: null,
        doctorName: "",
        departmentId: null,
        scheduleTime: null,
        amTimeQuantum: false,//上午
        pmTimeQuantum: false,//下午
        amNumberSource: null,
        pmNumberSource: null
      },
      keywords: null,
      current: 1,
      size: 10,
      count: 0
    };
  },
  methods: {
    searchUsers() {
      this.current = 1;
      this.listDoctorSchedule();
    },
    sizeChange(size) {
      this.size = size;
      this.listDoctorSchedule();
    },
    currentChange(current) {
      this.current = current;
      this.listDoctorSchedule();
    },
    openModel(doctorSchedule, type) {
      if (doctorSchedule) {
        this.addScheduleShow = false;
        //传入的type是1就是新增医生排班
        switch (type) {
          case 1:
            this.doctorScheduleForm = {
              doctorId: null,
              doctorName: "",
              departmentId: null,
              scheduleTime: null,
              amTimeQuantum: false,//上午
              pmTimeQuantum: false,//下午
              amNumberSource: null,
              pmNumberSource: null
            };
            this.$refs.scheduleTitle.innerHTML = "医生排班设置";
            break;
            //传入的type是2就是修改医生
          case 2:
            this.doctorScheduleForm = JSON.parse(JSON.stringify(doctorSchedule));
            this.$refs.scheduleTitle.innerHTML = this.doctorScheduleForm.doctorName + "医生排班设置";
            break;
        }
      } else {
        this.$refs.scheduleTitle.innerHTML = "医生排班设置";
        this.addScheduleShow = true;
        this.doctorScheduleForm = {
          doctorId: null,
          doctorName: "",
          departmentId: null,
          scheduleTime: null,
          amTimeQuantum: false,//上午
          pmTimeQuantum: false,//下午
          amNumberSource: null,
          pmNumberSource: null
        };
      }
      this.addScheduleShow = true;
    },
    cascaderChange() {
      this.departmentId = this.departmentIdList[1];
    },
    saveDoctorSchedule() {
      console.log(this.doctorScheduleForm);
      this.axios
          .post("/api/admin/saveDoctorSchedule",this.doctorScheduleForm)
          .then(({data}) => {
            if (data.flag) {
              this.$notify.success({
                title: "成功",
                message: data.message
              });
              this.listDoctorSchedule();
            } else {
              this.$notify.error({
                title: "失败",
                message: data.message
              });
            }
            this.addScheduleShow = false;
          })
    },
    changeWeekDate() {
      this.listDoctorSchedule();
    },
    listDoctorSchedule() {
      this.axios
          .get("/api/admin/schedule/week", {
            params: {
              current: this.current,
              size: this.size,
              keywords: this.keywords,
              status: this.weekDate,
              departmentId: this.departmentId
            }
          })
          .then(({data}) => {
            this.doctorScheduleList = data.data.recordList;
            this.count = data.data.count;
            this.loading = false;
          });
    },
    listDepartments() {
      this.axios
          .get("/api/admin/listDepartment", {})
          .then(({data}) => {
            this.departmentList = data.data;
          });
    },
    getNumberSource(weekDay, timeQuantum, data) {
      if(data == null){
        return '休息'
      } else {
        data = data.weeklySchedule
      }

      for (let item of data) {
        if (weekDay == item.weekDay && timeQuantum == item.timeQuantum) {
          return item.numberSource;
        }
      }
      return '休息';
    }
  },
};
</script>

<style>
.is-selected {
  color: #1989FA;
}

.dialog-body {
  display: flex;
  flex-flow: row nowrap;
}

.dialog-body-schedule {
  width: 700px;
  margin-right: 30px;
}

.dialog-body-calendar {
}
</style>
